package com.primeton.fbsearch.msdoc;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.textmining.extraction.TextExtractor;
import org.textmining.extraction.word.WordTextExtractorFactory;

import com.primeton.fbsearch.framework.DocumentHandler;
import com.primeton.fbsearch.framework.DocumentHandlerException;

public class TextMiningWordDocHandler implements DocumentHandler {

  public Document getDocument(InputStream is)
    throws DocumentHandlerException {

    
    Document doc = null;
    try {
    	WordTextExtractorFactory factory = new WordTextExtractorFactory();    	
    TextExtractor te = factory.textExtractor(is);
    
    String bodyText =  te.getText();   
    
    if ((bodyText != null) && (bodyText.trim().length() > 0)) {
         doc = new Document();
        doc.add(new Field("body", bodyText,Field.Store.YES, Field.Index.TOKENIZED,	Field.TermVector.WITH_POSITIONS_OFFSETS));
       
      }
    
    }
    catch (Exception e) {
      throw new DocumentHandlerException(
        "Cannot extract text from a Word document", e);
    }

    return doc;
  }

  public static void main(String[] args) throws Exception {
	  
	  String file = "D:\\OpenSource\\search_data\\全文检索lucene环境搭建.doc";
	  
    TextMiningWordDocHandler handler =
      new TextMiningWordDocHandler();
    Document doc = handler.getDocument(
      new FileInputStream(new File(file)));
    System.out.println(doc);
  }
}
